//
//	Provide a very simple set of functions to 
//	implement a draggable "splitter" bar. 
//	All this does is to draw the checkered drag-bar
//

#include "gripper.h"

static int  oldy = -4;
static BOOL fMoved = FALSE;
static BOOL fDragMode = FALSE;

void DrawXorBar(HDC hdc, int x1, int y1, int width, int height)
{
	static WORD _dotPatternBmp[8] = { 0x00aa, 0x0055, 0x00aa, 0x0055, 0x00aa, 0x0055, 0x00aa, 0x0055};

	HBITMAP hbm;
	HBRUSH  hbr, hbrushOld;

	hbm = CreateBitmap(8, 8, 1, 1, _dotPatternBmp);
	hbr = CreatePatternBrush(hbm);
	
	SetBrushOrgEx(hdc, x1, y1, 0);
	hbrushOld = (HBRUSH)SelectObject(hdc, hbr);
	
	PatBlt(hdc, x1, y1, width, height, PATINVERT);
	
	SelectObject(hdc, hbrushOld);
	
	DeleteObject(hbr);
	DeleteObject(hbm);
}


LRESULT Splitter_OnLButtonDown(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	POINT pt;
	HDC hdc;
	RECT rect;

	pt.x = (short)LOWORD(lParam);  // horizontal position of cursor 
	pt.y = (short)HIWORD(lParam);

	GetWindowRect(hwnd, &rect);
	OffsetRect(&rect, -rect.left, -rect.top);
	ScreenToClient(hwnd, &pt);

	if(pt.y < 0) pt.y = 0;
	if(pt.y > rect.bottom-4) 
	{
		pt.y = rect.bottom-4;
	}

	fDragMode = TRUE;

	SetCapture(hwnd);

	hdc = GetWindowDC(hwnd);
	DrawXorBar(hdc, 1,pt.y, rect.right-2,4);
	ReleaseDC(hwnd, hdc);
	
	oldy = pt.y;
		
	return 0;
}


LRESULT Splitter_OnLButtonUp(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	RECT rect;

	POINT pt;
	pt.x = (short)LOWORD(lParam);  // horizontal position of cursor 
	pt.y = (short)HIWORD(lParam);

	if(fDragMode == FALSE)
		return 0;
	
	GetWindowRect(hwnd, &rect);
	OffsetRect(&rect, -rect.left, -rect.top);

	if(pt.y < 0) pt.y = 0;
	if(pt.y > rect.bottom-4) 
	{
		pt.y = rect.bottom-4;
	}

	hdc = GetWindowDC(hwnd);
	DrawXorBar(hdc, 1,oldy, rect.right-2,4);			
	ReleaseDC(hwnd, hdc);

	oldy = pt.y;

	fDragMode = FALSE;

	ReleaseCapture();

	return 0;
}

LRESULT Splitter_OnMouseMove(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	RECT rect;

	POINT pt;

	if(fDragMode == FALSE) return 0;

	pt.x = (short)LOWORD(lParam);  // horizontal position of cursor 
	pt.y = (short)HIWORD(lParam);

	GetWindowRect(hwnd, &rect);
	OffsetRect(&rect, -rect.left, -rect.top);

	if(pt.y < 0) pt.y = 0;
	if(pt.y > rect.bottom-4) 
	{
		pt.y = rect.bottom-4;
	}

	if(pt.y != oldy && wParam & MK_LBUTTON)
	{
		hdc = GetWindowDC(hwnd);
		DrawXorBar(hdc, 1,oldy, rect.right-2,4);
		DrawXorBar(hdc, 1,pt.y, rect.right-2,4);
			
		ReleaseDC(hwnd, hdc);

		oldy = pt.y;
	}

	return 0;
}
